# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=ossec-hids-agent
_target=${pkgname/ossec-hids-/}
pkgver=3.7.0
pkgrel=0
pkgdesc="Open Source Host-based Intrusion Detection System"
url="https://www.ossec.net/"
arch="all !aarch64 !armhf !armv7 !s390x"
license="GPL-2.0-only WITH openssl-exception"
depends="inotify-tools procps ossec-hids-openrc"
makedepends="linux-headers bsd-compat-headers openssl-dev>3 \
libevent-dev inotify-tools-dev findutils file zlib-dev pcre2-dev \
lua5.3-dev geoip-dev czmq-dev"
checkdepends="check-dev subunit-dev"
subpackages="$pkgname-doc"
pkgusers="ossec ossecm ossecr"
pkggroups="ossec"
source="$pkgname-$pkgver.tar.gz::https://github.com/ossec/ossec-hids/archive/$pkgver.tar.gz
	$pkgname.logrotate
	musl_lack_of_a_out_h.patch
	makefile.patch
	config"

builddir="$srcdir"/ossec-hids-$pkgver

export CFLAGS="$CFLAGS -fcommon"

export _make_args="V=1 USER_INSTALL_TYPE=$_target USER_NO_STOP=yes \
	USER_DIR=/var/ossec USER_BINARYINSTALL=x USE_GEOIP=yes USE_ZEROMQ=yes \
	LUA_ENABLE=yes USE_INOTIFY=yes PCRE2_SYSTEM=yes USE_SYSTEMD=no"

# loonarch64 does not support pcre2-jit
case $CARCH in
        loongarch64) _make_args="$_make_args USE_PCRE2_JIT=n"
esac

build() {
	cd "$builddir"/src
	make clean
	make TARGET=$_target PREFIX=/var/ossec $_make_args
}

check() {
	cd "$builddir"/src
	make test PREFIX=/var/ossec $_make_args
}

package() {
	install -Dm644 -D "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname
	install -Dm644 -D etc/ossec-$_target.conf "$pkgdir"/etc/ossec-$_target.conf

	mkdir -p "$pkgdir"/var/ossec/etc
	cat << EOF > "$pkgdir"/var/ossec/etc/ossec-init.conf
DIRECTORY="/var/ossec"
VERSION="$(cat src/VERSION)"
DATE="$(date)"
TYPE="$_target"
EOF

	set -- $pkgusers
	cd "$builddir"/src
	find "$pkgdir" -user nobody -exec chown 524 '{}' ';'
	find "$pkgdir" -user mail -exec chown 525 '{}' ';'
	find "$pkgdir" -user daemon -exec chown 526 '{}' ';'
	find "$pkgdir" -group nobody -exec chgrp 525 '{}' ';'

	make install  TARGET="$_target" PREFIX="$pkgdir"/var/ossec $_make_args
}

doc() {
	cd "$builddir"
	pkgdesc="Documentation for $pkgname"
	mkdir -p "$subpkgdir"/usr/share/doc/$pkgname
	cp -a doc/* \
	  "$subpkgdir"/usr/share/doc/$pkgname
}

sha512sums="
ba76ae4e36158fda4359365733852ba43c71307dd42f957237b30680193f1a2d4f0704047f842075b2c720cd166550ea9531ded0fa5aceb1bc3fb9c227bae4aa  ossec-hids-agent-3.7.0.tar.gz
6cdf4852feabfdd043405e2570bb9a3013eb11c1865e9178fb67a019717d44fb0fedba05ab74c4334a1bae0a0c45912213dd7d6c7e1eab31853d40beea7596a0  ossec-hids-agent.logrotate
4e076581cc3977c527f30da6c43552db18bc35ea7b745c1504f4d15ebfbcef42c9604804af28fc90744a85f847a0f0c5bf991476cae71e3d860adb7cfa33a63b  musl_lack_of_a_out_h.patch
dc41b348f5f5467a8fc2018042b45824e98d393ae0c6632ac0c3022324dfd51f827c0a4ea4d6e201242e1a67769feae421070884a26c145d5a0e4cbde7dbcd47  makefile.patch
5213936052ed3adf8d9bb36d044386e2decf85ad59e7d5a69b4b73c708ea779dc03049c3429e9b658efceb347b950b0192ca4f66bb56a3101c2016f106dcc287  config
"
